/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.xpath.parser.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import moxl.reader.LocateableReader;
import moxl.reader.XPathExpressionReader;
import moxl.tokenizer.TokenStream;
import moxl.tokenizer.Tokenizer;
import moxl.tokenizer.WhitespaceFilteringTokenStream;
import moxl.xpath.parser.Operand;
import moxl.xpath.parser.XPathParser;
import moxl.xpath.parser.types.Expression;
import moxl.xpath.parser.types.FunctionCall;
import moxl.xpath.parser.types.PathExpression;
import moxl.xpath.parser.types.XPathLiteralOperand;
import moxl.xpath.parser.types.XPathNumberOperand;
import moxl.xpath.tokenizer.StartState;

import org.junit.Before;
import org.junit.Test;

public class ExpressionParserTest2
{
	private XPathParser parser;
	private TokenStream tstream;
	private Expression expr;

	@Before
	public void setUp() throws Exception
	{
		LocateableReader reader = new XPathExpressionReader("(fn(@type)+fn2(child::section/para,'true'))>=20");
		Tokenizer toker = new Tokenizer(reader, StartState.getInstance());
		tstream = new WhitespaceFilteringTokenStream(toker);
		parser = new XPathParser();
		expr = parser.parseExpressionFromStream(tstream);
	}

	@Test
	public void expressionNotNull()
	{
		assertNotNull(expr);
	}

	@Test
	public void firstOperandType()
	{
		assertEquals(Expression.class, expr.getFirstOperand().getClass());
	}

	@Test
	public void secondOperandType()
	{
		assertEquals(XPathNumberOperand.class, expr.getSecondOperand().getClass());
	}

	@Test
	public void secondOperandValue()
	{
		XPathNumberOperand number = (XPathNumberOperand) expr.getSecondOperand();
		assertEquals(20d, number.getNumericValue(), 0.1);
	}

	@Test
	public void operator()
	{
		assertEquals(">=", expr.getOperator().getImage());
	}

	@Test
	public void firstOperandFirstOperandType()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		assertEquals(FunctionCall.class, firstOperand.getFirstOperand().getClass());
	}

	@Test
	public void firstOperandFirstOperandFunctionName()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		FunctionCall functionCall = (FunctionCall) firstOperand.getFirstOperand();
		assertEquals("fn", functionCall.getFunctionName());
	}

	@Test
	public void firstOperandFirstOperandFunctionArgType()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		FunctionCall functionCall = (FunctionCall) firstOperand.getFirstOperand();
		Operand[] args = functionCall.getArguments();
		assertEquals(1, args.length);
		assertEquals(PathExpression.class, args[0].getClass());
	}

	@Test
	public void firstOperandSecondOperandType()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		assertEquals(FunctionCall.class, firstOperand.getSecondOperand().getClass());
	}

	@Test
	public void firstOperandSecondOperandFunctionName()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		FunctionCall functionCall = (FunctionCall) firstOperand.getSecondOperand();
		assertEquals("fn2", functionCall.getFunctionName());
	}

	@Test
	public void firstOperandSecondOperandFunctionArgTypes()
	{
		Expression firstOperand = (Expression) expr.getFirstOperand();
		FunctionCall functionCall = (FunctionCall) firstOperand.getSecondOperand();
		Operand[] args = functionCall.getArguments();
		assertEquals(2, args.length);
		assertEquals(PathExpression.class, args[0].getClass());
		assertEquals(XPathLiteralOperand.class, args[1].getClass());
	}

}
